import systemdemo from '@/components/ts/systemdemo'
import { computed } from 'vue'
import lodash from 'lodash'
import { load_curves_sheet } from '@/components/ts/修正曲线'
import correct from '@/components/ts/correct'
import { storeToRefs } from 'pinia'
import { useCCPPStore } from '@/store'

const store = useCCPPStore()

const {
  selectedUnitType,
  correct_paras,
  selectedGasFlowType,
  pulseToVolumeRatio, designLHV, designFlowRate, designFlowRateUnit,
  origin_sp,
  sp
} = storeToRefs(store)
const printToTerm = store.printToTerm


const spec1 = computed(() => {
  let res
  if (selectedUnitType.value == '二拖一') {
    res = [
      ['', '燃机1发电功率', 'MW'],
      ['', '燃机2发电功率', 'MW'],
      ['', '汽机发电功率', 'MW'],
      ['', '燃机1励磁功率', 'MW'],
      ['', '燃机2励磁功率', 'MW'],
      ['', '汽机励磁功率', 'MW']
    ]
  } else if (selectedUnitType.value == '一拖一单轴') {
    res = [
      ['', '发电功率', 'MW'],
      ['', '发电机励磁功率', 'MW']

    ]
  } else {
    res = [
      ['', '燃机发电功率', 'MW'],
      ['', '汽机发电功率', 'MW'],
      ['', '燃机发电机励磁功率', 'MW'],
      ['', '汽机发电机励磁功率', 'MW']
    ]
  }
  return res
})

// 联合循环计算按钮点击事件
export const handleCalculate = () => {
  printToTerm(`开始${selectedUnitType.value}联合循环计算...`)
  systemdemo.clear_sheet('联合循环计算')
  systemdemo.create_new_sheet('联合循环计算')

  if (!store.dataScanned) {
    store.scanData()
  }

  systemdemo.add_title_region('联合循环计算')
  let correct_region = correct.correct_index(correct_paras.value) // 根据需要进行的修正项生成计算区域的标题

  let spec3 = [
    ['', '机组净功率', 'MW'],
    ['', '', ''],
    ['天然气流量计算', '天然气压力', 'MPa'],
    ['', '天然气温度', '℃']
  ]
  if (selectedGasFlowType.value === '涡轮流量计_脉冲计数') {
    spec3 = [...spec3, ...[
      ['', '流量计脉冲数', 'Pulses/h'],
      ['', '天然气体积流量（工况）', 'm3/h'],
      ['', '天然气密度（工况）', 'kg/m3'],
      ['', '天然气质量流量', 'kg/h'],
      ['', '标况下天然气密度', 'kg/Nm3'],
      ['', '标况下天然气体积流量', 'Nm3/h'],
      ['', '天然气摩尔质量', 'kg/kmol'],
      ['', '天然气摩尔流量', 'kmol/h']
    ]]
  } else if (selectedGasFlowType.value === '涡轮流量计_体积流量') {
    spec3 = [...spec3, ...[
      ['', '标况下天然气体积流量', 'Nm3/h'],
      ['', '标况下天然气密度', 'kg/Nm3'],
      ['', '天然气质量流量', 'kg/h'],
      ['', '天然气摩尔质量', 'kg/kmol'],
      ['', '天然气摩尔流量', 'kmol/h']
    ]]
  } else {
    spec3 = [...spec3, ...[
      ['', '天然气质量流量', 'kg/h'],
      ['', '天然气摩尔质量', 'kg/kmol'],
      ['', '天然气摩尔流量', 'kmol/h']
    ]]
  }
  spec3 = [...spec3, ...[
    ['', '', ''],
    ['试验热耗率计算', '天然气显热焓', 'kJ/kg'],
    ['', '天然气低位热值', 'kJ/kg'],
    ['', '燃气显热能量', 'kJ/h'],
    ['', '燃气输入热量', 'kJ/h'],
    ['', '联合循环热耗量', 'kJ/h'],
    ['', '联合循环试验热耗率', 'kJ/kWh'],
    ['', '联合循环试验热效率', '%'],
    ['', '联合循环气耗', 'Nm3/kWh'],
    ['', '折算发电煤耗', 'g/kWh']
  ]]
  // console.log(correct_region)
  let data = [...spec1.value, ...spec3, ...correct_region]
  let start_row = systemdemo.getLastRowIndexOfSheet('联合循环计算')
  // console.log('开始写入数据', start_row)
  systemdemo.set_cells(start_row + 2, 1, data)
  const curveRegions = load_curves_sheet('修正曲线')
  console.log('扫描后的测点信息为', sp.value)
  correct.correct_CCPP(start_row + 2, data.length, '联合循环计算', sp.value, curveRegions,
    Number(pulseToVolumeRatio.value), Number(designLHV.value), Number(designFlowRate.value), designFlowRateUnit.value
  )
  // correct.correct_CCPP2(start_row + 2, data.length, '联合循环计算', sp, curveRegions,
  //   Number(pulseToVolumeRatio.value), Number(designLHV.value), Number(designFlowRate.value), designFlowRateUnit.value
  // )


  correct.summary_correct_curve(systemdemo.getLastRowIndexOfSheet('联合循环计算') + 1, '联合循环计算', sp)
  systemdemo.auto_fit_columns()
  console.log('...')
}